package com.example.gmwang.washapplication;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.example.gmwang.washapplication.utils.Constants;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.Calendar;

import okhttp3.Call;

/**
 * Created by gmwang on 2017/11/24.
 */

public class WashViewActivity extends Activity {
    ConstraintLayout rlView;
    private TextView tvShowDialog;
    private TextView timeDialog;
    private Calendar cal;
    private int year, month, day;
    private int hour, minute;
    private String dateString = "0000";
    private String timeString = "0000";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_washview);
        rlView = findViewById(R.id.rlView);
        Button recordButton = findViewById(R.id.recordId);
        Button recordListButton = findViewById(R.id.recordList);
        tvShowDialog = (TextView) findViewById(R.id.tvShowDialog);
        timeDialog = (TextView) findViewById(R.id.timeDialog);
//        Toast.makeText(getApplicationContext(), "show off", Toast.LENGTH_SHORT);
        //获取当前日期
        getDate();
        tvShowDialog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                switch (view.getId()) {
                    case R.id.tvShowDialog:
                        DatePickerDialog.OnDateSetListener listener = new DatePickerDialog.OnDateSetListener() {
                            @Override
                            public void onDateSet(DatePicker arg0, int year, int month, int day) {
                                dateString = year + "-" + (++month) + "-" + day;
                                tvShowDialog.setText(dateString);      //将选择的日期显示到TextView中,因为之前获取month直接使用，所以不需要+1，这个地方需要显示，所以+1
                            }
                        };
                        DatePickerDialog dialog = new DatePickerDialog(WashViewActivity.this, 0, listener, year, month, day);//后边三个参数为显示dialog时默认的日期，月份从0开始，0-11对应1-12个月
                        dialog.show();
                        break;
                    default:
                        break;
                }
            }
        });
        timeDialog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                switch (view.getId()) {
                    case R.id.timeDialog:
                        TimePickerDialog.OnTimeSetListener listener = new TimePickerDialog.OnTimeSetListener() {
                            @Override
                            public void onTimeSet(TimePicker timePicker, int hour, int minute) {
                                timeString = hour + ":"+minute;
                                timeDialog.setText(timeString);
                            }
                        };
                        TimePickerDialog dialog = new TimePickerDialog(WashViewActivity.this, 0, listener, hour, minute,true);
                        dialog.show();
                        break;
                    default:
                        break;
                }
            }
        });
        recordButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.e("recordButton",dateString);
                if(TextUtils.isEmpty(tvShowDialog.getText())){
                    Toast.makeText(getApplicationContext(), "请先选择日期！！！！！！", Toast.LENGTH_LONG).show();
                    return ;
                }
                if(TextUtils.isEmpty(timeDialog.getText())){
                    Toast.makeText(getApplicationContext(), "请先选择时间！！！！！！", Toast.LENGTH_LONG).show();
                    return ;
                }
                String url = Constants.url+"addRecord";
                StringBuffer sb = new StringBuffer();
//                sb.append("a=").append("1").append("&");
//                AesEncrypt.GetAesMore(sb);
                OkHttpUtils.get().url(url).addParams("cuuid","66666").addParams("washDate", dateString).addParams("washTime",timeString).build().execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.e("onError:", e.toString());
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.e("onResponse:", response);
                        JSONObject responseJson = (JSONObject) JSONObject.parse(response);
                        String code = responseJson.getString("code");
                        String message = responseJson.getString("message");
                        //保存成功
                        if("100".equals(responseJson.get("code"))){
                            Intent intent = new Intent(WashViewActivity.this,WashViewListActivity.class);
                            startActivity(intent);
                        }else{
                            Toast.makeText(getApplicationContext(), code+","+responseJson.get("message"), Toast.LENGTH_LONG).show();
                        }
                    }
                });
            }
        });
        recordListButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(WashViewActivity.this,WashViewListActivity.class);
                startActivity(intent);
            }
        });
    }

    //获取当前日期
    private void getDate() {
        cal = Calendar.getInstance();
        year = cal.get(Calendar.YEAR);       //获取年月日时分秒
        Log.i("wxy", "year" + year);
        month = cal.get(Calendar.MONTH);   //获取到的月份是从0开始计数
        day = cal.get(Calendar.DAY_OF_MONTH);
        hour = cal.get(Calendar.HOUR_OF_DAY);
        minute = cal.get(Calendar.MINUTE);
    }


    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha
     */
    public void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        if (bgAlpha == 1) {
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//不移除该Flag的话,在有视频的页面上的视频会出现黑屏的bug
        } else {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//此行代码主要是解决在华为手机上半透明效果无效的bug
        }
        getWindow().setAttributes(lp);
    }
}